package org.misalen.web.server.filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * 
 * 自定义 HttpServlet(重写)
 * 
 * @author guochao
 *
 */
public class ParameterRequestWrapper extends HttpServletRequestWrapper {

	/** 储存表单 key-value */
	private Map<String, Object> params = new HashMap<String, Object>();
	/** 储存文件流 key-value */
	private ByteArrayInputStream bais;
	/** 编码 */
	private final static Charset charset = Charset.forName("UTF-8");

	public ParameterRequestWrapper(HttpServletRequest request) {
		super(request);
		try {
			setCharacterEncoding(charset.name());
		} catch (UnsupportedEncodingException e) {

		}
		bais = new ByteArrayInputStream(getBodyString(request).getBytes(charset));
		Map<?, ?> map = request.getParameterMap();
		for (Iterator<?> iter = map.entrySet().iterator(); iter.hasNext();) {
			@SuppressWarnings("rawtypes")
			Map.Entry element = (Map.Entry) iter.next();
			params.put(element.getKey().toString(), element.getValue());
		}
	}

	/**
	 * 添加 params
	 * 
	 * @param key
	 * @param value
	 */
	public void setParameter(String key, Object value) {
		params.put(key, value);
	}

	/**
	 * 覆盖文件流
	 * 
	 * @param body
	 */
	public void setInputStream(String body) {
		if (body != null) {
			bais = new ByteArrayInputStream(body.getBytes(charset));
		} else {
			bais = null;
		}

	}

	@Override
	public String getParameter(String name) {
		String result = null;
		Object v = params.get(name);
		if (v != null) {
			result = v.toString();
		}
		return result;
	}

	@Override
	public Map<?, ?> getParameterMap() {
		return params;
	}

	@Override
	public Enumeration<String> getParameterNames() {
		return new Vector<String>(params.keySet()).elements();
	}

	@Override
	public BufferedReader getReader() throws IOException {
		return new BufferedReader(new InputStreamReader(getInputStream()));
	}

	@Override
	public ServletInputStream getInputStream() throws IOException {

		return new ServletInputStream() {
			@Override
			public int read() throws IOException {
				return bais.read();
			}
		};
	}

	public String getInputString() {
		return getBodyString(this);
	}

	private String getBodyString(ServletRequest request) {
		StringBuilder sb = new StringBuilder();
		InputStream inputStream = null;
		BufferedReader reader = null;
		try {
			inputStream = request.getInputStream();
			reader = new BufferedReader(new InputStreamReader(inputStream, charset));
			String line = "";
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

}
